package utils;

import java.io.File;
import java.util.ArrayList;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import jxl.Cell;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * 把log信息写入手机根目录，文件名为：Mytestlogs.text
 * 
 * @author Administrator
 *
 */
public class MyTestResult {
	String model = Build.MODEL.replace(" ", "_");// 获取当前设备型号
	/**
	 * 输入测试结果与统计到excel文件<br>
	 * 当前方法为：在手机根目录创建文件夹MyTestResult，并在文件夹下创建文件model + "_" + "MyTestResult.xls"<br>
	 * 并在excel文件中写入固定的测试信息
	 * s
	 * @throws Exception
	 */
	public void creatTestResultExcel() throws Exception {
		String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();// 设备根目录的绝对路劲
		String linebreak = File.separator;
		
		File file$ = new File(baseDir + linebreak + "MyTEST");
		file$.mkdirs();

		File file = new File(file$.getAbsolutePath() + linebreak + model + "_" + "MyTestResult.xls");
		if(file.exists()){
			file.delete();
			file.createNewFile();
		}else{
			file.createNewFile();
		}
		WritableWorkbook wbook = Workbook.createWorkbook(file);
		WritableSheet sheet = wbook.createSheet("自动化测试结果与统计", 0);

		// 合并单元格，第二列到第4列 第一行到第5行
		sheet.mergeCells(1, 0, 3, 0);
		sheet.mergeCells(1, 1, 3, 1);
		sheet.mergeCells(1, 2, 3, 2);
		sheet.mergeCells(1, 3, 3, 3);
		sheet.mergeCells(1, 4, 3, 4);
		// 标记单元格
		WritableFont font =new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.DARK_BLUE2);
		WritableCellFormat fontFormat=new WritableCellFormat(font);
		Label label0 = new Label(0, 0, "测试设备",fontFormat);
		Label label1 = new Label(0, 1, "测试开始时间",fontFormat);
		Label label2 = new Label(0, 2, "测试结束时间",fontFormat);
		Label label3 = new Label(0, 3, "测试用例总数",fontFormat);
		Label label4 = new Label(0, 4, "测试失败率",fontFormat);
		Label label5 = new Label(0, 5, "测试用例编号",fontFormat);
		Label label6 = new Label(1, 5, "测试项或操作步骤",fontFormat);
		Label label7 = new Label(2, 5, "期望结果",fontFormat);
		Label label8 = new Label(3, 5, "测试结果",fontFormat);
		Label label9 = new Label(1, 0, model + "==》Android" + Build.VERSION.RELEASE,fontFormat);
		Label label10 = new Label(1, 1, CurrentTime.getCurrentTime() + "",fontFormat);

		// 写入数据
		sheet.addCell(label0);
		sheet.addCell(label1);
		sheet.addCell(label2);
		sheet.addCell(label3);
		sheet.addCell(label4);
		sheet.addCell(label5);
		sheet.addCell(label6);
		sheet.addCell(label7);
		sheet.addCell(label8);
		sheet.addCell(label9);
		sheet.addCell(label10);
		wbook.write();// 写入
		wbook.close();
	}

	/**
	 * 提供给测试用例使用的输出测试结果到excel文件的方法
	 * 
	 * @param number
	 *            测试编号（测试用例）
	 * @param operatingSteps
	 *            测试项或操作步骤
	 * @param expected
	 *            期望结果
	 * @param actual
	 *            测试结果 pass/fail
	 * @throws Exception
	 */
	public void printTestResultToExcel(String number, String operatingSteps, String expected , String actual)
			throws Exception {

		ArrayList<String> list = new ArrayList<String>();
		list.add(number);
		list.add(operatingSteps);
		list.add(expected);
		list.add(actual);

		String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "MyTEST"
				+ File.separator + model + "_" + "MyTestResult.xls";// 设备根目录的绝对路劲
		File file = new File(baseDir);

		Workbook wbook = Workbook.getWorkbook(file);
		WritableWorkbook book = Workbook.createWorkbook(file, wbook);
		WritableSheet sheet = book.getSheet(0);

		int columnum = sheet.getColumns();// 得到列数
		int rownum = sheet.getRows();// 得到行数

		for (int j = 0; j < columnum; j++) {
			// 循环写入
			WritableFont font=new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
			WritableCellFormat fontFormat=new WritableCellFormat(font);
			
			if(list.get(j).equals("测试过程操作失败或出现异常")){
				Label labe = new Label(j, rownum, list.get(j),fontFormat);
				sheet.addCell(labe);
			}else{
				Label labe$ = new Label(j, rownum, list.get(j));
				sheet.addCell(labe$);
			}
		}
		book.write();
		book.close();
	}

	/**
	 * 测试结果统计 输入测试结束时间、测试用例总数和测试失败率
	 * 
	 * @throws Exception
	 */
	public void ResultStatistics() throws Exception {
		String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "MyTEST"
				+ File.separator + model + "_" + "MyTestResult.xls";// 设备根目录的绝对路劲
		File file = new File(baseDir);

		Workbook wbook = Workbook.getWorkbook(file);
		WritableWorkbook book = Workbook.createWorkbook(file, wbook);
		WritableSheet sheet = book.getSheet(0);
		//设置字体样式
		WritableFont font=new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,true,UnderlineStyle.NO_UNDERLINE,Colour.RED);
		WritableCellFormat fontFormat=new WritableCellFormat(font);
		WritableFont font$=new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,true,UnderlineStyle.NO_UNDERLINE,Colour.GREEN);
		WritableCellFormat fontFormat$=new WritableCellFormat(font$);
		WritableFont font$$ =new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.DARK_BLUE2);
		WritableCellFormat fontFormat$$=new WritableCellFormat(font$$);
		// 更新测试结束时间
		int rows = sheet.getRows();
		Label lable1 = new Label(1, 2, CurrentTime.getCurrentTime() + "",fontFormat$$);// 测试结束时间
		// 统计失败的条数
		int failCounts = 0;
		for (int i = 6; i < rows; i++) {
			Cell cell = sheet.getCell(3, i);
			if (cell.getContents().equals("fail")) {
				Label lable=new Label(3,i,"fail",fontFormat);
				sheet.addCell(lable);
				failCounts++;
			}
		}
		//统计错误的条数
		int errorCounts=0;
		for (int i = 6; i < rows; i++) {
			Cell cell = sheet.getCell(3, i);
			if (cell.getContents().equals("error")) {
				Label lable=new Label(3,i,"error",fontFormat);
				sheet.addCell(lable);
				errorCounts++;
			}
		}
		//统计成功的条数
		int successCounts = 0;
		for (int i = 6; i < rows; i++) {
			Cell cell = sheet.getCell(3, i);
			if (cell.getContents().equals("pass")) {
				Label lable=new Label(3,i,"pass",fontFormat$);
				sheet.addCell(lable);
				successCounts++;
			}
		}
		Log.d("yiqijiao", "fail总数："+failCounts);
		Log.d("yiqijiao", "error总数："+errorCounts);
		Log.d("yiqijiao", "pass总数："+successCounts);
		//测试失败率
		double scanle=(double)(Math.round((failCounts+errorCounts)*10000/(failCounts+successCounts+errorCounts))/100.0);
		Log.d("yiqijiao", "失败率="+scanle + "%");
		Label lable3 = new Label(1, 4, scanle+ "%",fontFormat);
		// 测试用例总数
		Label lable2 = new Label(1, 3, (failCounts+successCounts+errorCounts) + "",fontFormat$$);
		
		sheet.addCell(lable1);
		sheet.addCell(lable2);
		sheet.addCell(lable3);
		book.write();
		book.close();
	}
}